package ua.kpi_java_training.zabrodskyi.module3;

/**
 * Initialization of all elements in environment becomes.
 * 
 * @author Maksym Zabrodskyi
 * @version 25-Jun-2013
 */
public final class Initialization {

	private Environment environment;

	public Initialization(Environment environment) {
		this.environment = environment;
	}

	/**
	 * Does initialization of all units.
	 * 
	 * @param settings
	 *            - settings of environment.
	 */
	public void doInitializeAllElements(Settings settings) {
		initializePredators(settings.getNumberPredators());
		initializeVictims(settings.getNumberVictims());
		initializeBarriers(settings.getNumberBarriers());
	}

	/**
	 * Does initialization of predators in environment.
	 * 
	 * @param number
	 *            - quantity of predators.
	 */
	private void initializePredators(int number) {
		for (int counter = 0; counter < number; counter++) {
			if (environment.hasFreeCell()) {
				environment.add(ElementType.PREDATOR);
			}
		}
	}

	/**
	 * Does initialization of victims in environment.
	 * 
	 * @param number
	 *            - quantity of victims.
	 */
	private void initializeVictims(int number) {
		for (int counter = 0; counter < number; counter++) {
			if (environment.hasFreeCell()) {
				environment.add(ElementType.VICTIM);
			}
		}
	}

	/**
	 * Does initialization of barriers in environment.
	 * 
	 * @param number
	 *            - quantity of barriers.
	 */
	private void initializeBarriers(int number) {
		for (int counter = 0; counter < number; counter++) {
			if (environment.hasFreeCell()) {
				environment.add(ElementType.BARRIER);
			}
		}
	}

}
